/**
 * 
 */
package org.statewalker.parser.base;

import org.statewalker.tokenizer.CharStream;
import org.statewalker.tokenizer.CompositeTokenizer;
import org.statewalker.tokenizer.ITokenizer;
import org.statewalker.validator.IValidatorListener;
import org.statewalker.validator.ValidatorFactory;

/**
 * @author kotelnikov
 */
public abstract class ParserFactory {

    private TokenizerProvider fTokenizerProvider = new TokenizerProvider();

    protected ValidatorFactory fValidatorFactory;

    public ParserFactory(ValidatorFactory validatorFactory) {
        fValidatorFactory = validatorFactory;
        bindTokenizers();
    }

    protected abstract void bindTokenizers();

    public ValidatorFactory getValidatorFactory() {
        return fValidatorFactory;
    }

    public Parser newParser(CharStream stream, IValidatorListener... listeners) {
        Parser parser = new Parser(
            fValidatorFactory,
            fTokenizerProvider,
            stream,
            listeners);
        return parser;
    }

    protected void setTokenizer(String key, ITokenizer tokenizer) {
        fTokenizerProvider.registerTokenizer(key, tokenizer);
    }

    protected void setTokenizers(String key, ITokenizer... tokenizers) {
        CompositeTokenizer tokenizer = new CompositeTokenizer();
        for (ITokenizer t : tokenizers) {
            tokenizer.addTokenizer(t);
        }
        fTokenizerProvider.registerTokenizer(key, tokenizer);
    }

}